package com.lsf.studentmanagerup.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 李树飞
 */
@WebServlet(name = "LogoutServlet", urlPatterns = "/logout")
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //清除登录信息，转发到登录页面
        System.out.println("删除登录信息，跳转到login.jsp");
        //从session中删除
        request.getSession().removeAttribute("user");
        //删除Cookie
        Cookie userCookie = new Cookie("user", null);
        userCookie.setMaxAge(0);
        response.addCookie(userCookie);

        //重定向到登录页面
        request.getRequestDispatcher("login.jsp").forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
